package com.lyy.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyy.common.constant.Token;
import com.lyy.common.shiro.JWTUtil;
import com.lyy.common.utils.Constants;
import com.lyy.common.utils.UUIDUtils;
import com.lyy.system.dao.SysUserDao;
import com.lyy.system.pojo.SysUser;
import com.lyy.system.service.SysUserService;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 用户表(SysUser)表服务实现类
 *
 * @author Yw
 * @since 2021-02-01 17:12:01
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUser> implements SysUserService {


    /**
     * 添加用户表
     *
     * @param sysUser
     */
    @Override
    public void createSysUser(SysUser sysUser) {
        sysUser.setCreateDate(new Date());
        sysUser.setDelFlag("0");
        baseMapper.insert(sysUser);
    }

    /**
     * 根据主键修改用户表
     *
     * @param sysUser
     */
    @Override
    public void updateSysUser(SysUser sysUser) {
        sysUser.setUpdateDate(new Date());
        baseMapper.update(sysUser, new UpdateWrapper<SysUser>()
                .eq("id", sysUser.getId())
        );
    }

    /**
     * 根据主键删除用户表
     *
     * @param id 用户id
     * @return
     */
    @Override
    public void deleteSysUser(SysUser sysUser) {
        baseMapper.delete(new UpdateWrapper<SysUser>()
                .eq("id", sysUser.getId())
        );
    }

    /**
     * 根据主键查询用户表详情
     *
     * @param id 用户id
     * @return
     */
    @Override
    public SysUser getSysUser(SysUser sysUser) {
        return baseMapper.selectOne(new QueryWrapper<SysUser>()
                .eq("id", sysUser.getId())
        );
    }

    /**
     * 批量添加用户表
     *
     * @param sysUsers
     */
    @Override
    public int batchInsert(List<SysUser> sysUsers) {
        return baseMapper.batchInsert(sysUsers);
    }


    /**
     * 批量修改用户表
     *
     * @param sysUsers
     */
    @Override
    public int batchUpdate(List<SysUser> sysUsers) {
        return baseMapper.batchUpdate(sysUsers);
    }

    /**
     * 查询用户表列表
     *
     * @param sysUser
     */
    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser) {
        return baseMapper.selectSysUserList(sysUser);
    }

    /**
     * 查询用户表数量
     *
     * @param sysUser
     */
    @Override
    public int selectSysUserCount(SysUser sysUser) {
        return baseMapper.selectSysUserCount(sysUser);
    }


    /**
     * 导入用户表excel
     *
     * @param sysUsers
     */
    @Override
    public List<Map<String, String>> importSysUserExcel(List<SysUser> sysUsers) {
        List<Map<String, String>> mapList = new ArrayList<>();
        sysUsers.forEach(sysUser -> {
            sysUser.setId(UUIDUtils.getUUIDTWO());
            Map<String, String> map = new HashMap<>();
            map.put("status", "0");
            map.put("reason", "导入成功");
            mapList.add(map);
        });
        batchInsert(sysUsers);
        return mapList;
    }


    /**
     * 加密密码
     *
     * @param credentials 原文
     * @param salt
     */
    @Override
    public String passwordEncoder(String credentials, String salt) {
        Object object = new SimpleHash("MD5", credentials, salt, Constants.HASH_ITERATIONS);
        return object.toString();
    }


    /**
     * 获取Token
     *
     * @param username
     * @param secret
     * @return
     */
    @Override
    public Token getToken(String username, String secret) {
        String key = JWTUtil.sign(username, secret);
        return new Token(key, DateUtils.addSeconds(new Date(), (int) Constants.EXPIRE_TIME / 1000));
    }

    /**
     * 获取Token
     *
     * @param username
     * @param secret
     * @return
     */
    @Override
    public Token getLongToken(String username, String secret) {
        String key = JWTUtil.signLong(username, secret);
        return new Token(key, DateUtils.addSeconds(new Date(), (int) Constants.EXPIRE_LONG_TIME / 1000));
    }


    /**
     * 通过用户名获取用户
     *
     * @param username
     * @return
     */
    @Override
    public SysUser getUserByUserName(String username) {
        return baseMapper.selectOne(new QueryWrapper<SysUser>()
                .eq("username", username)
                .eq("del_flag", "0")
        );
    }

}